<?php
namespace app\index\controller;
use app\index\controller\Base;
use think\Db;
use think\facade\Request;

class Login extends Base
{
    //公司新闻页面
    public function index()
    {
      
        $ob = Db::table('f_test')->where('id',1)->find();
        $num = $ob["num"];
     
        if($num>0){
            $num --;
            $data = ['userId' =>  $num];
            Db::startTrans();
            try {
                Db::name('test2')->insert($data);
                Db::name('test')->where('id', 1)->update(['num' =>  $num]);
                // 提交事务
                Db::commit();
                return json(["status"=>'0',"msg"=>"抢购成功"]);
            } catch (\Exception $e) {
                // 回滚事务
                Db::rollback();
                return json(["status"=>'-1',"msg"=>"抢购失败"]);
            }
         
        }else{
            return json(["status"=>'0',"msg"=>"已经没库存了"]);
        }
        
    }
}